﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OF.Security;
using OF.Utility;

namespace Passport.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            try
            {

                //调用登录
                //1、验证参数是否有效
                //DefaultAuthenticator.ValidSignInParams(System.Web.HttpContext.Current.Request);
                string appsignInUrl = "";
                //SignInPageData pd = new SignInPageData();

                ////2、提取用户设置（cookie中）                   
                //pd.LoadFromCookie();
                //3、尝试自动登录应用（不同应用之前切换时，如用户在A应用上已经登录，访问B应用是应该自动登录到B）
                appsignInUrl = DefaultAuthenticator.AutoSignIn();

                if (!string.IsNullOrEmpty(appsignInUrl) && appsignInUrl.Contains("http")) //完成自动登录
                {
                    if (appsignInUrl.IndexOf("login", StringComparison.InvariantCultureIgnoreCase) == -1)
                        //防止login页面循环调用
                    {
                        return Redirect(appsignInUrl);
                    }
                }
            }
            catch (BusinessException ex)
            {
                //记录错误

            }
            return View();
        }
        [HttpPost]
        public ActionResult Login(FormCollection form)
        {
            var userName = RequestUtility.GetString("UserName");
            var passpord = RequestUtility.GetString("Password");
            bool bSuccess = false;
            string msg;
            bSuccess = DefaultAuthenticator.SignIn(userName, passpord, out msg);
            if (bSuccess)
            {
                Response.Redirect(msg);
            }
            else
            {
                ViewBag.ErrMsg = msg;
                ViewBag.ShowError = true;
            }
            return View();
        }

        /// <summary>
        /// 注销页面
        /// </summary>
        public void LogOff()
        {
            IList<string> logOffUrls = DefaultAuthenticator.LogOffAllAPP();
            var loginUrl=Url.Action("Login");
            string ReturnUrl = Request.QueryString["RedirectUrl"]?? loginUrl;
            HttpResponse Response = System.Web.HttpContext.Current.Response;
            Response.Clear();
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Type", "text/html");
            Response.Write(@"
<div>正在注销。。。</div>
    <div style='display:none'>");
            foreach (string item in logOffUrls)
            {
                Response.Write(string.Format("<img src='{0}' />", item));
            }
            Response.Write("</div>");
            Response.Write(@"
<script language='javascript'>
    window.onload = function () {
       window.location.href = '");
            Response.Write(ReturnUrl);
            Response.Write(@"';
    }
</script>");
        }

    }
}